{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XI7Ced7GrJWD"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/t81_558_deep_learning/blob/master/assignments/assignment_yourname_class9.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Important Note\n",
    "\n",
    "This assignment is from the older (Keras-based) version of this course and is no longer used for my class. You can find the current asignments here: [updated assignments](https://github.com/jeffheaton/app_deep_learning/tree/main/assignments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9MexQXyMrJWF"
   },
   "source": [
    "# T81-558: Applications of Deep Neural Networks\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), School of Engineering and Applied Science, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/).\n",
    "\n",
    "**Module 9 Assignment: Transfer Learning**\n",
    "\n",
    "**Student Name: Your Name**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "paeWGos3rJWG"
   },
   "source": [
    "# Assignment Instructions\n",
    "\n",
    "This assignment gives you the chance to explore some of the most advanced pretrained networks available.  Keras comes with around 20 pretrained neural networks built-in.  You can use these networks right out of the box without modification or extend these networks through transfer learning.  For this assignment, I will show you how you can explore these networks and examine their structure. This technique can be a great learning aid to see the structure of some of the most advanced neural networks.\n",
    "\n",
    "To create one of the pretrained neural networks in Keras use the **application** package.  For example, you can create the **Xception** neural network with the following command:\n",
    "\n",
    "```\n",
    "net = tf.keras.applications.Xception()\n",
    "```\n",
    "\n",
    "To see the neural network structure issue the **summary** command:\n",
    "\n",
    "```\n",
    "net.summary()\n",
    "```\n",
    "\n",
    "The **dir** command will tell you what methods and properties are available for the neural network.  You will use these functions to extract data from this structure.  For example, to see the first layer:\n",
    "\n",
    "```\n",
    "net.layers[0]\n",
    "```\n",
    "\n",
    "To see what type the first layer is:\n",
    "\n",
    "```\n",
    "type(net.layers[0])\n",
    "```\n",
    "\n",
    "To see the internals of that layer:\n",
    "\n",
    "```\n",
    "dir(net.layers[0])\n",
    "```\n",
    "\n",
    "Use these sort of commands to build a table that looks similar to this:\n",
    "\n",
    "|name|input|output|layers|max_layer_wgt|wgt_count|\n",
    "|---|---|---|---|---|---|\n",
    "|Xception|299 x 299 x 3|1000|134|3.0M|21.8M\n",
    "|VGG16|224 x 224 x 3|1000|23|98.0M|131.9M\n",
    "|VGG19|224 x 224 x 3|1000|26|98.0M|137.0M\n",
    "|...|...|...|...|...|...\n",
    "\n",
    "The meanings of these columns are:\n",
    "\n",
    "* **name** - The name of the network.\n",
    "* **input**\t- The count/structure of input neurons.\n",
    "* **output** - The count/structure of output neurons.\n",
    "* **layers** - The count of layers.\n",
    "* **max_layer_wgt**\t- The maximum number of weights in any layer. (as a string)\n",
    "* **wgt_count** - The total count of weights. (as a string)\n",
    "\n",
    "Note, that I do request you to output weight counts a string, such as 10M.  I provide a helper function for this.  Also note, that I do request the input structure, such as 128 x 128 x 3.  You should create a helper function of your own to format this output.\n",
    "\n",
    "Report on the following pretrained neural networks:\n",
    "\n",
    "* Xception\n",
    "* VGG16\n",
    "* VGG19\n",
    "* ResNet50\n",
    "* ResNet101\n",
    "* ResNet152V2\n",
    "* InceptionV3\n",
    "* InceptionResNetV2\n",
    "* MobileNet\n",
    "* MobileNetV2\n",
    "* DenseNet121\n",
    "* DenseNet169\n",
    "* DenseNet201\n",
    "* NASNetMobile\n",
    "* NASNetLarge\n",
    "* EfficientNetB7\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1MMTkYKDrJWH"
   },
   "source": [
    "# Google CoLab Instructions\n",
    "\n",
    "If you are using Google CoLab, it will be necessary to mount your GDrive so that you can send your notebook during the submit process. Running the following code will map your GDrive to ```/content/drive```."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Tw2yXkqWrJWI"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from google.colab import drive\n",
    "    drive.mount('/content/drive', force_remount=True)\n",
    "    COLAB = True\n",
    "    print(\"Note: using Google CoLab\")\n",
    "except:\n",
    "    print(\"Note: not using Google CoLab\")\n",
    "    COLAB = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "frUMKm2JrJWO"
   },
   "source": [
    "# Assignment Submit Function\n",
    "\n",
    "You will submit the ten programming assignments electronically.  The following **submit** function can be used to do this.  My server will perform a basic check of each assignment and let you know if it sees any underlying problems. \n",
    "\n",
    "**It is unlikely that should need to modify this function.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "iYsU9ObUrJWO"
   },
   "outputs": [],
   "source": [
    "import base64\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import requests\n",
    "import PIL\n",
    "import PIL.Image\n",
    "import io\n",
    "\n",
    "# This function submits an assignment.  You can submit an assignment as much as you like, only the final\n",
    "# submission counts.  The paramaters are as follows:\n",
    "# data - List of pandas dataframes or images.\n",
    "# key - Your student key that was emailed to you.\n",
    "# no - The assignment class number, should be 1 through 1.\n",
    "# source_file - The full path to your Python or IPYNB file.  This must have \"_class1\" as part of its name.  \n",
    "# .             The number must match your assignment number.  For example \"_class2\" for class assignment #2.\n",
    "def submit(data,key,no,source_file=None):\n",
    "    raise Exception(\"If you are a current student, you are using an old version of this assignment. Refer to: https://github.com/jeffheaton/app_deep_learning/tree/main/assignments\")\n",
    "    if source_file is None and '__file__' not in globals(): raise Exception('Must specify a filename when a Jupyter notebook.')\n",
    "    if source_file is None: source_file = __file__\n",
    "    suffix = '_class{}'.format(no)\n",
    "    if suffix not in source_file: raise Exception('{} must be part of the filename.'.format(suffix))\n",
    "    with open(source_file, \"rb\") as image_file:\n",
    "        encoded_python = base64.b64encode(image_file.read()).decode('ascii')\n",
    "    ext = os.path.splitext(source_file)[-1].lower()\n",
    "    if ext not in ['.ipynb','.py']: raise Exception(\"Source file is {} must be .py or .ipynb\".format(ext))\n",
    "    payload = []\n",
    "    for item in data:\n",
    "        if type(item) is PIL.Image.Image:\n",
    "            buffered = BytesIO()\n",
    "            item.save(buffered, format=\"PNG\")\n",
    "            payload.append({'PNG':base64.b64encode(buffered.getvalue()).decode('ascii')})\n",
    "        elif type(item) is pd.core.frame.DataFrame:\n",
    "            payload.append({'CSV':base64.b64encode(item.to_csv(index=False).encode('ascii')).decode(\"ascii\")})\n",
    "    r= requests.post(\"https://api.heatonresearch.com/assignment-submit\",\n",
    "        headers={'x-api-key':key}, json={ 'payload': payload,'assignment': no, 'ext':ext, 'py':encoded_python})\n",
    "    if r.status_code==200:\n",
    "        print(\"Success: {}\".format(r.text))\n",
    "    else: print(\"Failure: {}\".format(r.text))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "id": "2eeWZysZrJWS",
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "# Assignment #9 Sample Code\n",
    "\n",
    "The following code provides a starting point for this assignment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bTUii3ujrJWT"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "from scipy.stats import zscore\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Activation, Dropout\n",
    "from tensorflow.keras.models import load_model\n",
    "import pandas as pd\n",
    "import io\n",
    "import requests\n",
    "import numpy as np\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import KFold\n",
    "import sklearn\n",
    "from sklearn.linear_model import Lasso\n",
    "\n",
    "# This is your student key that I emailed to you at the beginnning of the semester.\n",
    "key = \"H3B554uPhc3f8kirGGBYA7cYuDOamhXM87OY6QH1\"  # This is an example key and will not work.\n",
    "\n",
    "# You must also identify your source file.  (modify for your local setup)\n",
    "file='/content/drive/MyDrive/Colab Notebooks/assignment_class9.ipynb'  # Google CoLab\n",
    "# file='C:\\\\Users\\\\jeffh\\\\projects\\\\t81_558_deep_learning\\\\assignments\\\\assignment_yourname_class9.ipynb'  # Windows\n",
    "# file='/Users/jeff/projects/t81_558_deep_learning/assignments/assignment_yourname_class9.ipynb'  # Mac/Linux\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "E44qdq0PpGBT"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tensorflow as tf\n",
    "\n",
    "lst_names = []\n",
    "lst_input_count = []\n",
    "lst_all_weights = []\n",
    "lst_max_weights = []\n",
    "lst_input = []\n",
    "lst_output = []\n",
    "lst_layers = []\n",
    "lst_sort = []\n",
    "\n",
    "# This function is based on the following:\n",
    "# https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size\n",
    "def sizeof_fmt(num, suffix='B'):\n",
    "  for unit in ['','K','M','G','T','P','E','Z']:\n",
    "    if abs(num) < 1024.0:\n",
    "      return \"%3.1f%s\" % (num, unit)\n",
    "    num /= 1024.0\n",
    "  return \"%.1f%s\" % (num, 'Y')\n",
    "\n",
    "\n",
    "\n",
    "def process_network(name,net):\n",
    "    pass\n",
    "    # Add code here\n",
    "\n",
    "\n",
    "process_network(\"Xception\", tf.keras.applications.Xception())\n",
    "process_network(\"VGG16\", tf.keras.applications.VGG16())\n",
    "process_network(\"VGG19\", tf.keras.applications.VGG19())\n",
    "# Add code here\n",
    "\n",
    "df = pd.DataFrame()\n",
    "df['name'] = lst_names\n",
    "df['input'] = lst_input\n",
    "df['output'] = lst_output\n",
    "df['layers'] = lst_layers\n",
    "df['max_layer_wgt'] = lst_max_weights\n",
    "df['wgt_count'] = lst_all_weights\n",
    "\n",
    "submit(source_file=file,data=[df],key=\"y75zXVg7BSaB9FrVznQCA3dSLcKmY1Rp8h00I1QS\",no=9)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "colab": {
   "collapsed_sections": [],
   "name": "assignment_class9.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3.10 (tensorflow)",
   "language": "python",
   "name": "tensorflow"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
